﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using TallyBook.BLL.Manger.Expend;
using TallyBook.BLL.Service;
using TallyBook.BLL.Service.Imp;

namespace TallyBook.UI
{
    public partial class AddAccountForm : Form
    {
        private readonly IAccountService<Account> _account;

        private readonly List<TextBox> _textBoxs = new List<TextBox>();

        public AddAccountForm(IAccountService<Account> account)
        {
            _account = account;
            InitializeComponent();
        }

        public Action RefreshMainForm { get; set; }


        private void AddAccountForm_Load(object sender, EventArgs e)
        {
            _textBoxs.Add(textAccount);
            _textBoxs.Add(textName);
            _textBoxs.Add(textInitMoney);
            _textBoxs.Add(textCurrentMoney);
            _textBoxs.Add(textComment);
            Member();
        }

        public void Member()
        {
            var account = new AccountService();
            comboBoxType.DataSource = null;
            List<AccountType> get = account.GetName();
            comboBoxType.DataSource = get;
            comboBoxType.DisplayMember = "Name";
            comboBoxType.ValueMember = "Id";
        }


        /// <summary>
        ///     添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click_1(object sender, EventArgs e)
        {

            if (_textBoxs.Any(item => string.IsNullOrEmpty(item.Text)))
            {
                MessageBox.Show(@"信息未填完整");
                return;
            }

            var detection = new Detection();
            var list = _textBoxs.Where(item => item.Name == "textAccount" || item.Name == "textInitMoney" || item.Name == "textCurrentMoney").ToArray();
            if (list.Any(detection.DetectionMoney))
            {
                MessageBox.Show(@"含非法字符");
                return;
            }

            long accounttypeid = long.Parse(comboBoxType.SelectedValue.ToString());
            long accounid = long.Parse(textAccount.Text);
            string accountname = textName.Text;
            decimal initmoney = decimal.Parse(textInitMoney.Text);
            decimal currentmoney = decimal.Parse(textCurrentMoney.Text);
            string comment = textComment.Text;
            DateTime time = DateTime.Now;

            //数据验证逻辑

           
            switch (Detection())
            {
                case 1:
                    MessageBox.Show(@"账户填写错误");
                    return;
                case 2:
                    MessageBox.Show(@"名字过长");
                    return;
                case 3:
                    MessageBox.Show(@"金额出现非法字符");
                    return;
            }

            if (_account.Insert(accounid, accounttypeid, accountname, initmoney, currentmoney, comment, time) !=
                0)
            {
                MessageBox.Show(@"添加成功");
                RefreshMainForm();
                Hide();
            }
            //GDatas(arryList);
        }

        /// <summary>
        ///     重置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click_1(object sender, EventArgs e)
        {
            _textBoxs.ForEach(item => item.Text = null);
        }

        /// <summary>
        ///     检测
        /// </summary>
        /// <returns></returns>
        private int Detection()
        {
            if (_textBoxs[0].Text.Any(item => item > 57 || item < 48))
                return 1;
            if (_textBoxs[1].Text.Length > 5)
                return 2;
            if (_textBoxs[2].Text.Any(item => item > 57 || item < 48))
                return 3;
            return _textBoxs[2].Text.Any(item => item > 57 || item < 48) ? 4 : 0;
        }
    }
}